package com.ge.healthcare.autosc.ui.prodiagsscheduler.service.impl;

import java.util.Date;
import java.util.List;

import com.ge.healthcare.autosc.common.database.dao.CfgProDiagsScheduleDAO;
import com.ge.healthcare.autosc.common.database.pojo.CfgProDiagsSchedule;
import com.ge.healthcare.autosc.common.database.pojo.HstProdiagsSchedule;
import com.ge.healthcare.autosc.ui.prodiagsscheduler.dto.ScheduleBean;
import com.ge.healthcare.autosc.ui.prodiagsscheduler.service.intf.CfgProDiagsScheduleService;
import com.ge.healthcare.autosc.ui.prodiagsscheduler.service.intf.HstProdiagsScheduleService;
public class CfgProDiagsScheduleServiceImpl implements CfgProDiagsScheduleService {

	private CfgProDiagsScheduleDAO cfgProDiagsScheduleDAO;
	
	public void setCfgProDiagsScheduleDAO(
			CfgProDiagsScheduleDAO cfgProDiagsScheduleDAO) {
		this.cfgProDiagsScheduleDAO = cfgProDiagsScheduleDAO;
	}
	private HstProdiagsScheduleService hstProdiagsScheduleService;
	
	public void setHstProdiagsScheduleService(
			HstProdiagsScheduleService hstProdiagsScheduleService) {
		this.hstProdiagsScheduleService = hstProdiagsScheduleService;
	}
	public void updateSchedule(ScheduleBean scheduleBean) {
		cfgProDiagsScheduleDAO.updateSchedule(converScheduleToProDiagSchedule(scheduleBean));
	}
	
	private CfgProDiagsSchedule converScheduleToProDiagSchedule(ScheduleBean scheduleBean){
		CfgProDiagsSchedule cfgProDiagsSchedule;
		List<CfgProDiagsSchedule> cfgProDiagsSchedulesModelType=cfgProDiagsScheduleDAO.getRequestByModelType(scheduleBean.getModelType());
		List<CfgProDiagsSchedule> cfgProDiagsSchedulesSystemID=cfgProDiagsScheduleDAO.getRequestBySystemID(scheduleBean.getSystemId());
		if(!cfgProDiagsSchedulesModelType.isEmpty()){
			cfgProDiagsSchedule= cfgProDiagsSchedulesModelType.get(0);
			HstProdiagsSchedule hstProdiagsSchedule = new HstProdiagsSchedule();
			hstProdiagsSchedule.setDay(cfgProDiagsSchedule.getDay());
			hstProdiagsSchedule.setHour(cfgProDiagsSchedule.getHour());
			hstProdiagsSchedule.setMinute(cfgProDiagsSchedule.getMinute());
			hstProdiagsSchedule.setWeek(cfgProDiagsSchedule.getWeek());
			hstProdiagsSchedule.setSystemId(cfgProDiagsSchedule.getSystemId());
			hstProdiagsSchedule.setModelType(cfgProDiagsSchedule.getModelType());
			hstProdiagsSchedule.setTaskName(cfgProDiagsSchedule.getTaskName());
			hstProdiagsSchedule.setUpdatedDate(new Date());
			hstProdiagsScheduleService.saveOrUpdate(hstProdiagsSchedule);
		}
		else if(!cfgProDiagsSchedulesSystemID.isEmpty()){
			cfgProDiagsSchedule = cfgProDiagsSchedulesSystemID.get(0);
			HstProdiagsSchedule hstProdiagsSchedule = new HstProdiagsSchedule();
			hstProdiagsSchedule.setDay(cfgProDiagsSchedule.getDay());
			hstProdiagsSchedule.setHour(cfgProDiagsSchedule.getHour());
			hstProdiagsSchedule.setMinute(cfgProDiagsSchedule.getMinute());
			hstProdiagsSchedule.setWeek(cfgProDiagsSchedule.getWeek());
			hstProdiagsSchedule.setSystemId(cfgProDiagsSchedule.getSystemId());
			hstProdiagsSchedule.setModelType(cfgProDiagsSchedule.getModelType());
			hstProdiagsSchedule.setTaskName(cfgProDiagsSchedule.getTaskName());
			hstProdiagsSchedule.setUpdatedDate(new Date());
			hstProdiagsScheduleService.saveOrUpdate(hstProdiagsSchedule);
		}
		else{
		cfgProDiagsSchedule = new CfgProDiagsSchedule();
		}
		cfgProDiagsSchedule.setDay(scheduleBean.getDay());
		cfgProDiagsSchedule.setHour(scheduleBean.getHour());
		cfgProDiagsSchedule.setIterations(scheduleBean.getIterations());
		cfgProDiagsSchedule.setMinute(scheduleBean.getMinute());
		cfgProDiagsSchedule.setModelType(scheduleBean.getSystemId());
		cfgProDiagsSchedule.setSystemId(scheduleBean.getSystemId());
		cfgProDiagsSchedule.setTaskName(scheduleBean.getTaskName());
		cfgProDiagsSchedule.setWeek(scheduleBean.getWeek());
		cfgProDiagsSchedule.setUpdatedDate(new Date());
		return cfgProDiagsSchedule;
	}
	
	

}
